using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/**
 * 敌人坦克脚本
 */
public class Enemy : MonoBehaviour
{
    // 移动速度
    public float moveSpeed = 3f;

    // 子弹角度
    private Vector3 bulletEulerAngles = Vector3.zero;

    // 方向枚举
    private enum Direction { Down, Right, Up, Left };
    private Direction direction = Direction.Down;

    // 引用
    private SpriteRenderer spriteRenderer;
    private Rigidbody2D rb;

    // 坦克图片数组   上0 右1  下2 左3         
    public Sprite[] tankSprites;
    
    // 子弹预制件
    public GameObject bulletPrefab; // 确保在编辑器中指定了这个预制件
    
    // 爆炸预制件
    public GameObject explosionPrefab;

    // 计时器
    private float attackTimer = 0f;
    private float changeDirectionTimer = 0f;

    // 初始化组件引用
    private void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        rb = GetComponent<Rigidbody2D>();

        // 初始化方向为向下
        InitializeDirection(Direction.Down);
    }

    // 初始化方向
    private void InitializeDirection(Direction initialDirection)
    {
        direction = initialDirection;

        // 根据初始方向更新图像
        UpdateSprite(Vector3.down);
    }

    // Start is called before the first frame update
    private void Start()
    {
    }

    // Update is called once per frame
    private void Update()
    {
        // 攻击的时间间隔
        if (attackTimer >= 2f)
        {
            Attack(); // 坦克攻击
            attackTimer = 0f; // 重置计时器
        }
        else
        {
            attackTimer += Time.deltaTime; // 计时器累加
        }
    }

    private void FixedUpdate()
    {
        Move(); // 坦克移动方法
    }

    private void Move()
    {
        // 改变方向的时间计时器
        if (changeDirectionTimer >= 4f)
        {
            direction = GetRandomDirection(); // 随机选择一个新的方向
            changeDirectionTimer = 0f; // 重置计时器
        }
        else
        {
            changeDirectionTimer += Time.fixedDeltaTime; // 计时器累加
        }

        // 根据方向移动
        Vector3 movement = Vector3.zero;
        switch (direction)
        {
            case Direction.Down:
                movement = Vector3.down;
                break;
            case Direction.Right:
                movement = Vector3.right;
                break;
            case Direction.Up:
                movement = Vector3.up;
                break;
            case Direction.Left:
                movement = Vector3.left;
                break;
        }

        // 移动坦克
        // 将 Vector3 转换为 Vector2
        rb.MovePosition(rb.position + new Vector2(movement.x, movement.y) * moveSpeed * Time.fixedDeltaTime);

        // 更新坦克图像
        UpdateSprite(movement);
    }

    // 随机获取一个方向
    private Direction GetRandomDirection()
    {
        int randomIndex = Random.Range(0, 4);
        switch (randomIndex)
        {
            case 0: return Direction.Down;
            case 1: return Direction.Right;
            case 2: return Direction.Up;
            case 3: return Direction.Left;
            default: return Direction.Down; // 默认向下
        }
    }

    // 更新坦克的图像
    private void UpdateSprite(Vector3 movement)
    {
        if (tankSprites.Length < 4) // 确保数组至少有4个元素
        {
            Debug.LogError("TankSprites array does not contain enough elements.");
            return;
        }

        if (movement == Vector3.down)
        {
            if (tankSprites.Length > 2)
            {
                spriteRenderer.sprite = tankSprites[2]; // 向下
                bulletEulerAngles = new Vector3(0, 0, 180);
            }
        }
        else if (movement == Vector3.right)
        {
            if (tankSprites.Length > 1)
            {
                spriteRenderer.sprite = tankSprites[1]; // 向右
                bulletEulerAngles = new Vector3(0, 0, -90);
            }
        }
        else if (movement == Vector3.up)
        {
            if (tankSprites.Length > 0)
            {
                spriteRenderer.sprite = tankSprites[0]; // 向上
                bulletEulerAngles = new Vector3(0, 0, 0);
            }
        }
        else if (movement == Vector3.left)
        {
            if (tankSprites.Length > 3)
            {
                spriteRenderer.sprite = tankSprites[3]; // 向左
                bulletEulerAngles = new Vector3(0, 0, 90);
            }
        }
    }

    // 坦克的攻击方法
    private void Attack()
    {
        // 确保 bulletPrefab 不为空
        if (bulletPrefab == null)
        {
            Debug.LogError("bulletPrefab is not assigned.");
            return;
        }

        // 获取当前坦克的角度
        Vector3 currentEulerAngles = transform.eulerAngles;

        // 计算子弹的旋转角度
        Vector3 newEulerAngles = currentEulerAngles + bulletEulerAngles;

        // 创建子弹
        Instantiate(bulletPrefab, transform.position, Quaternion.Euler(newEulerAngles));
    }

    // 敌人坦克的死亡方法
    public void Die()
    {
        PlayerMananger.Instance.playerScore++; // 增加玩家得分
        // 产生爆炸特效
        Instantiate(explosionPrefab, transform.position, transform.rotation);
        // 死亡
        Destroy(gameObject);
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Enemy"))
        {
            changeDirectionTimer = 4f; // 当碰撞到其他敌人时，改变方向
        }
    }
}